package org.kalai.unifypay.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.bean.Member;
import org.kalai.unifypay.util.CommonUtil;
import org.kalai.unifypay.util.DataDao;
import org.kalai.unifypay.util.StringUtil;

import com.alibaba.fastjson.JSON;

/**
 * 商户基本信息更新
 * @author zsl
 * @version 2016年9月11日 上午11:33:07
 */
@WebServlet(urlPatterns="/MemberUpdate")
public class MemberUpdate extends HttpServlet {
	private static final long serialVersionUID = 6453076416163097638L;
	private static final Logger logger = Logger.getLogger(MemberUpdate.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CommonUtil.responseObject(resp, Constant.ERROR_REQEST);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String> param = CommonUtil.validate(request, response);
		if (param != null) {//验证通过 返回参数集合
			logger.info("接收到商户更新请求："+param);
			Map<String, Object> result = CommonUtil.createRespMap("10", "系统异常");
			try {
				String memberNo = param.get("memberNo");
				String memberMobile = param.get("memberMobile");
				String acctNo = param.get("acctNo");
				String bankNo = param.get("bankNo");
				String bankName = param.get("bankName");
				if (StringUtil.isEmpty(memberNo, memberMobile, acctNo, bankNo, bankName)) {
					logger.error(String.format("业务参数错误：memberNo=%s, memberMobile=%s, acctNo=%s, bankNo=%s, bankName=%s", memberNo, memberMobile, acctNo, bankNo, bankName));
					throw new RuntimeException("#200-业务参数错误");
				}
				Member member = DataDao.getInstance().getMember(memberNo);
				if(member == null){
					logger.error(String.format("商户不存在：memberNo=%s", memberNo));
					throw new RuntimeException("#202-商户不存在");
				}
				boolean flag = DataDao.getInstance().modifyMember(memberNo, memberMobile, acctNo, bankNo, bankName);
				if (flag) {
					result = CommonUtil.createRespMap("00", "会员更新成功");
				} else {
					logger.error(String.format("会员更新失败：memberNo=%s, memberMobile=%s, acctNo=%s, bankNo=%s, bankName=%s", memberNo, memberMobile, acctNo, bankNo, bankName));
					result = CommonUtil.createRespMap("10", "会员更新失败");
				}
			} catch (Exception e) {
				String message = e.getMessage();
				if (message.startsWith("#")) {
					result = CommonUtil.createRespMap(message.substring(1, message.indexOf("-")), message.substring(message.indexOf("-") + 1));
				} else {
					result = CommonUtil.createRespMap("10", e.getMessage());
					e.printStackTrace();
				}
			} finally {
				logger.info("商户更新返回："+result);
				CommonUtil.responseObject(response, JSON.toJSONString(result));
			}
		}
	}

}
